import axios from 'axios'
import Vue from 'vue'
import store from '../store/store'
import { app } from '../main'
// import { baseUrl } from '../config/baseUrls'

import xml2js from 'xml2js' 

//xml->json
  //xml2js默认会把子子节点的值变为一个数组, explicitArray设置为false
let xmlParser = new xml2js.Parser({explicitArray : false, ignoreAttrs : true})

axios.defaults.timeout = 60000;
axios.defaults.headers.post['Content-Type'] = 'text/xml; charset=utf-8';
// axios.defaults.baseURL = baseUrl;
// axios.defaults.headers.post['Content-Type'] = 'text/xml; charset=utf-8';
// const oldUrl = axios.defaults.baseURL;

// //请求拦截
// axios.interceptors.request.use(function (config) {
//     return config
// }, function (error) {
//     return Promise.reject(error)
// })

// //响应拦截
// axios.interceptors.response.use(function (response) {
//     return response
// }, function () {
//     return Promise.reject()
// })

//封装axios的get请求
export function fetch(url, params, Origin) {
    if (Origin) {
        axios.defaults.baseURL = Origin;
    }
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {
                // axios.defaults.baseURL = oldUrl;
                resolve(response.data)
            })
            .catch((error) => {
                axios.defaults.baseURL = oldUrl;
                reject(error)
            })
    })
}

//封装axios的post请求
export function post(url, data = {}, Origin, openLoading) {
    return new Promise((resolve, reject) => {
        let paramStr = '';
        data.params.forEach((param,index)=>{
            paramStr+=`<bo:arg${index}>${param}</bo:arg${index}>`
        });
        let str = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bo="http://bo.baseinfo.business/"> 
           <soapenv:Header/>
       <soapenv:Body>
          <bo:${data.fnName}>
            ${paramStr}
          </bo:${data.fnName}>
       </soapenv:Body>
    </soapenv:Envelope>` ;

        //每一次http请求的唯一标识码
        const timestamp = new Date().getTime();
        axios.post(url, str)
            .then(response => {
                  xmlParser.parseString(response.data, function (err, result) {
                    console.log(result);
                 //将返回的结果再次格式化
                  let json= result['soap:Envelope']['soap:Body']['ns1:'+data.fnName + 'Response']['ns1:return'];
                  // function get_object_first_attribute(data){
                  //       for (var key in data)
                  //           return data[key];
                  //   }
                  //   function getKey(obj){
                  //           debugger
                  //       if(obj['ns1:return'] !== undefined && obj['ns1:return'] !== null){
                  //           return obj['ns1:return'];
                  //       }else {
                  //           getKey(get_object_first_attribute(obj));
                  //       }
                  //   }
                  //   let r = getKey(result);
                    resolve(JSON.parse(json));
                })
            })
            .catch((error) => {
                // axios.defaults.baseURL = oldUrl;
                reject(error);
            })
    })
}
